package dyyx;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.mvel2.MVEL;

import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import groovy.lang.GroovyShell;

public class PerfTest {

	public static void main(String[] args) throws Exception {	
		System.out.println("PerfTest");	
		
		int loop1 = 100;
		int loop2 = 1000000;
		String expression = "import dyyx.Utils; double x = Utils.add(a,b) ; y = (x + c*1.5) /3 ; return y";

		runTest(loop1,loop2,expression);
	
		
	}
	
	
	public static void runTest(int loop1,int loop2,String expression)throws Exception{
		
		Serializable compiledExpression = null;
		// 表达式编译
		compiledExpression = MVEL.compileExpression(expression);
		
		GroovyShell shell = null;
		
		
		GroovyClassLoader loader = null;	
	    loader = new GroovyClassLoader();
		Class groovyClass = loader.parseClass(expression);	
		System.out.println("groovyClass="+groovyClass);

		GroovyObject groovyObject = null;
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("a", 1);
		params.put("b", 2);
		params.put("c", 3);
		
		
		Binding context = new Binding();
        context.setProperty("a", 1);
        context.setProperty("b", 2);
        context.setProperty("c", 3);
        
        
        
        long mvelRunTime = 0;
        long mvelCompileRunTime = 0;
        long groovyRunTime = 0;
        long groovyCompileRunTime = 0;
        
        long start = 0;
        long end = 0;
        
        // mvel groovy 非编译执行的 性能太差 ，先注释掉

        for(int i=0;i<loop1;i++){
        	
        	// mvel
        	/*
        	start = System.currentTimeMillis();
        	for(int j=0;j<loop2;j++){
        		MVEL.eval(expression, params); 	
        	}
        	end = System.currentTimeMillis();
        	mvelRunTime = end - start;
        	System.out.println(mvelRunTime);
            */
        	
        	// mvel compile
        	start = System.currentTimeMillis();
        	for(int j=0;j<loop2;j++){
        		MVEL.executeExpression(compiledExpression, params);
        	}
        	end = System.currentTimeMillis();
        	mvelCompileRunTime = end - start;
        	System.out.println(mvelCompileRunTime);

        	
        	// groovy
        	/*
        	start = System.currentTimeMillis();
        	for(int j=0;j<loop2;j++){
        		shell = new GroovyShell(context);
                shell.evaluate(expression);   
        	}
        	end = System.currentTimeMillis();
        	groovyRunTime = end - start;
        	System.out.println(groovyRunTime);

        	*/
        	
        	// groovy compile
        	
        	final Object[] args = new Object[]{};
        	
        	start = System.currentTimeMillis();
        	for(int j=0;j<loop2;j++){
        		groovyObject = (GroovyObject) groovyClass.newInstance();	
        		// groovyObject 不可缓存	
        		groovyObject.setProperty("a", 1);
        		groovyObject.setProperty("b", 2);
        		groovyObject.setProperty("c", 3);	
        		groovyObject.invokeMethod("run", args);    
        	}
        	end = System.currentTimeMillis();
        	groovyCompileRunTime = end - start;
        	System.out.println(groovyCompileRunTime);

        	
        	
        	// System.out.println(mvelRunTime);
        	// System.out.println(mvelCompileRunTime);
        	// System.out.println(groovyRunTime);
        	// System.out.println(groovyCompileRunTime);
        	System.out.println("----------");
 
        // end loop1
        }
		
		
	}
	
	
	
	
	
}
